package com.mtz.controller;

import com.mtz.VO.ResultVO;
import com.mtz.dateobject.UserServer;
import com.mtz.service.UserServerService;
import com.mtz.utils.JsonUtil;
import com.mtz.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 煜赜
 * @date 2018/12/4 5:21
 */
@Api(description = "后台用户接口")
@RestController
@RequestMapping("/user/server")
public class UserServerController {

    @Autowired
    private UserServerService userServerService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private final long timeout = 60*3;

//    private RedisOperator redis = new RedisOperator();

    /**
     * 返回所有用户（除了超级管理员）
     * @return
     */
    @ApiOperation(value = "返回所有用户除了（除了超级管理员）", notes = "返回所有用户除了（除了超级管理员）")
    @GetMapping("/all")
    public ResultVO findAll() {
        List<UserServer> userServer = userServerService.findAll();
        return ResultVOUtil.success(userServer);
    }

    /**
     * 根据用户id查找用户
     * @param id 用户id
     * @return
     */
    @ApiOperation(value = "根据用户id查找用户", notes = "根据用户id查找用户")
    @GetMapping("/id")
    public ResultVO findByUserId(@ApiParam(value = "用户id",required = true)
                                   @RequestParam("id") Integer id) {
        UserServer userServer = userServerService.findById(id);
        return ResultVOUtil.success(userServer);
    }

    /**
     * 根据用户名查找用户
     * @param username 用户名
     * @return
     */
    @ApiOperation(value = "根据username查找用户", notes = "根据用户名查找用户")
    @GetMapping("/username")
    public ResultVO findByUsername(@ApiParam(value = "用户名",required = true)
                                       @RequestParam("username") String username) {
        UserServer userServer = userServerService.findByUsername(username);
        return ResultVOUtil.success(userServer);
    }

    /**
     * 根据用户类型查找用户
     * @param type 用户类型
     * @return
     */
    @ApiOperation(value = "根据type查找用户", notes = "根据用户类型查找用户")
    @GetMapping("/type")
    public ResultVO findByType(@ApiParam(value = "用户类型",required = true)
                                   @RequestParam("type") Integer type) {
        List<UserServer> userServerList = userServerService.findByType(type);
        return ResultVOUtil.success(userServerList);
    }

    /**
     * 登录检测
     * @param username 用户名
     * @param password 用户密码
     * @return
     */
    @ApiOperation(value = "登录检测", notes = "登录检测")
    @PostMapping("/login")
    public ResultVO checkLogin(@ApiParam(value = "用户名",required = true)
                                   @RequestParam("username") String username,
                               @ApiParam(value = "用户密码",required = true)
                                   @RequestParam("password") String password) {
        Map<String, String> userInfo = new HashMap<String, String>();

        UserServer userServer = userServerService.checkLogin(username, password);
        //保存用户的redis-session
        String uniqueToken = UUID.randomUUID().toString();
        //存到redis数据库  已经存在就更新token  key值是根据固定字符串+id唯一确定的
        redisTemplate.opsForValue().set(uniqueToken, JsonUtil.toJson(userServer), timeout, TimeUnit.SECONDS);

        userInfo.put("id", userServer.getId().toString());
        userInfo.put("token", uniqueToken);
        userInfo.put("username", userServer.getUsername());
        userInfo.put("type", userServer.getType().toString());
        userInfo.put("isDeleted", userServer.getIsDeleted().toString());

        return ResultVOUtil.success(userInfo);
    }

    /**
     * 登出
     * @param token token
     * @return
     */
    @ApiOperation(value = "退出登录", notes = "退出登录")
    @PostMapping("/loginOut")
    public ResultVO loginOut(@ApiParam(value = "token",required = true)
                               @RequestParam("token") String token) {
        redisTemplate.delete(token);
        return ResultVOUtil.success();
    }

    /**
     * 添加用户
     * @param username 用户名
     * @param password 密码
     * @param type 用户类型
     * @return
     */
    @ApiOperation(value = "添加用户", notes = "添加用户")
    @PostMapping("/add")
    public ResultVO addUser(@ApiParam(value = "用户名",required = true)
                                @RequestParam("username") String username,
                            @ApiParam(value = "用户密码",required = true)
                                @RequestParam("password") String password,
                            @ApiParam(value = "用户类型(不填则默认为4--普通用户)",required = false)
                                @RequestParam(value = "type", defaultValue = "4") Integer type) {

        userServerService.addUser(username, password, type);
        return ResultVOUtil.success();
    }

    /**
     * 修改用户名/密码
     * @param id       用户id
     * @param username 用户名
     * @param password 用户密码
     * @return
     */
    @ApiOperation(value = "修改用户名/密码", notes = "修改用户名/密码")
    @PostMapping("/edit")
    public ResultVO editUser(@ApiParam(value = "用户id",required = true)
                                 @RequestParam("id") Integer id,
                            @ApiParam(value = "用户名",required = true)
                                 @RequestParam("username") String username,
                             @ApiParam(value = "用户密码",required = true)
                                 @RequestParam("password") String password) {
        userServerService.editUser(id, username, password);
        return ResultVOUtil.success();
    }

    /**
     * 更改用户类型
     * @param id 用户id
     * @param type 用户类型
     * @return
     */
    @ApiOperation(value = "更改用户类型", notes = "更改用户类型/密码")
    @PostMapping("/changeType")
    public ResultVO changeUserType(@ApiParam(value = "用户id",required = true)
                                       @RequestParam("id") Integer id,
                                   @ApiParam(value = "用户类型",required = true)
                                       @RequestParam("type") Integer type) {
        userServerService.changeType(id, type);
        return ResultVOUtil.success();
    }

    /**
     * 激活/禁止用户
     * @param id 用户id
     * @param isDeleted 用户状态（0激活,1禁止）
     * @return
     */
    @ApiOperation(value = "更改用户状态", notes = "更改用户状态")
    @PostMapping("/changeIsDeleted")
    public ResultVO deleteById(@ApiParam(value = "用户id",required = true)
                                   @RequestParam("id") Integer id,
                               @ApiParam(value = "用户状态",required = true)
                                    @RequestParam("isDeleted") Integer isDeleted) {
        userServerService.changeIsDeledted(id, isDeleted);
        return ResultVOUtil.success();
    }
}
